package org.androidpn.activity;

import java.util.ArrayList;
import java.util.List;

import org.androidpn.R;
import org.androidpn.adapter.TagGridViewAdapter;
import org.androidpn.http.HttpApi;
import org.androidpn.model.CHotTag;
import org.androidpn.model.CUser;
import org.androidpn.model.CUserInfo;
import org.androidpn.model.NetModel;
import org.androidpn.utils.ConstanceUtil;
import org.androidpn.utils.JsonUtil;
import org.androidpn.utils.ToastUtil;
import org.androidpn.widget.CircleImageView;
import org.androidpn.widget.NoScrollGridView;

import com.android.volley.VolleyError;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class CompleteUserinfoActivity extends BaseActivity implements OnClickListener, OnItemClickListener {
	private ImageView ivBack;
	private EditText etName;
	private EditText etAge;
	private EditText etDescription;
	private EditText etHospital;
	private EditText etMajor;
	private CircleImageView ivHead;
	private RadioGroup rdSex;
	private TextView tvConfir;
	private CUserInfo cUserInfo;
	private int type = ConstanceUtil.TYPE_BINGREN;
	NoScrollGridView gridView1;
	NoScrollGridView gridView2;
	List<CHotTag> alltags = new ArrayList<CHotTag>();
	List<CHotTag> mytags = new ArrayList<CHotTag>();
	TagGridViewAdapter adapter1;
	TagGridViewAdapter adapter2;

	// 消息提示
	private static final int MSG_LOGIN_SHOW_LOADING = 0x401;
	private static final int MSG_LOGIN_HIDE_LOADING = 0x402;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.completeuserinfo_activity);
		ivBack = (ImageView) findViewById(R.id.iv_back);
		etName = (EditText) findViewById(R.id.et_name);
		etAge = (EditText) findViewById(R.id.et_age);
		etDescription = (EditText) findViewById(R.id.et_description);
		etHospital = (EditText) findViewById(R.id.et_hospital);
		etMajor = (EditText) findViewById(R.id.et_marjor);
		ivHead = (CircleImageView) findViewById(R.id.iv_head);
		rdSex = (RadioGroup) findViewById(R.id.radio_group);
		tvConfir = (TextView) findViewById(R.id.tv_confirm);
		gridView1 = (NoScrollGridView) findViewById(R.id.gridview1);
		gridView2 = (NoScrollGridView) findViewById(R.id.gridview2);
		ivHead.setBorderWidth(0);
		ivHead.setBorderColor(getResources().getColor(R.color.color_ffffff));
		CUser cUser = (CUser) getIntent().getSerializableExtra("cuser");
		type = cUser.getType();
		if (type == ConstanceUtil.TYPE_BINGREN) {
			etMajor.setVisibility(View.GONE);
			etHospital.setVisibility(View.GONE);
			etDescription.setVisibility(View.GONE);

		}

		ivBack.setOnClickListener(this);
		cUserInfo = new CUserInfo();
		cUserInfo.setSex(1);
		cUserInfo.setType(cUser.getType());
		cUserInfo.setTelephone(cUser.getTelephone());
		rdSex.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup arg0, int arg1) {
				// TODO Auto-generated method stub
				if (R.id.rd_man == arg0.getId()) {
					cUserInfo.setSex(1);
				} else {
					cUserInfo.setSex(0);
				}
			}
		});
		tvConfir.setOnClickListener(this);
		ivBack.setOnClickListener(this);
		adapter1 = new TagGridViewAdapter(this);
		adapter1.setTags(mytags);
		adapter2 = new TagGridViewAdapter(this);
		adapter2.setTags(alltags);
		gridView1.setAdapter(adapter1);
		gridView2.setAdapter(adapter2);
		gridView1.setOnItemClickListener(this);
		gridView2.setOnItemClickListener(this);
		GetAllTagResponseListenner responseListenner = new GetAllTagResponseListenner();
		HttpApi.getAllHotTagList(responseListenner, responseListenner);
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		switch (arg0.getId()) {
		case R.id.tv_confirm:
			if (!TextUtils.isEmpty(etName.getText().toString())) {
				cUserInfo.setName(etName.getText().toString());
			} else {
				ToastUtil.showShortToast(CompleteUserinfoActivity.this, "用户名信息不能为空");
				return;
			}
			if (!TextUtils.isEmpty(etAge.getText().toString())) {
				int age = Integer.parseInt(etAge.getText().toString());
				cUserInfo.setAge(age);
			} else {
				ToastUtil.showShortToast(CompleteUserinfoActivity.this, "用户名年龄不能为空");
				return;
			}

			if (type == ConstanceUtil.TYPE_YISHENG) {
				if (!TextUtils.isEmpty(etMajor.getText().toString())) {
					cUserInfo.setMajor(etMajor.getText().toString());
				} else {
					ToastUtil.showShortToast(CompleteUserinfoActivity.this, "专业领域信息不能为空");
					return;
				}
				if (!TextUtils.isEmpty(etHospital.getText().toString())) {
					cUserInfo.setHospital(etHospital.getText().toString());
				} else {
					ToastUtil.showShortToast(CompleteUserinfoActivity.this, "医院信息不能为空");
					return;
				}
				if (!TextUtils.isEmpty(etDescription.getText().toString())) {
					cUserInfo.setDescription(etDescription.getText().toString());
				} else {
					ToastUtil.showShortToast(CompleteUserinfoActivity.this, "描述信息不能为空");
					return;
				}
			}
			String tags = "";
			for (int i = 0; i < mytags.size(); i++) {
				if (i == 0) {
					tags += mytags.get(i).getTag();
				} else {
					tags += (ConstanceUtil.TAG + mytags.get(i).getTag());
				}
			}

			sendEmptydMessage(MSG_LOGIN_SHOW_LOADING);
			cUserInfo.setTag(tags);
			MyResponseListener listener = new MyResponseListener();
			HttpApi.saveUserinfo(cUserInfo, listener, listener);
			break;
		case R.id.iv_back:
			this.finish();
			break;
		case R.id.tv_login:
			Intent intent = new Intent(this, LoginActivity.class);
			startActivity(intent);
			break;
		}
	}

	class MyResponseListener
			implements com.android.volley.Response.Listener<String>, com.android.volley.Response.ErrorListener {

		@Override
		public void onResponse(String response) {
			// 隐藏提示框
			sendEmptydMessage(MSG_LOGIN_HIDE_LOADING);
			NetModel netModel = JsonUtil.parseObject(response, NetModel.class);
			if (netModel.getCode() == ConstanceUtil.REQUEST_SUCCESS) {
				ToastUtil.showShortToast(CompleteUserinfoActivity.this, netModel.getContent());
				Intent intentMain = new Intent(CompleteUserinfoActivity.this, LoginActivity.class);
				startActivity(intentMain);
				finish();
			} else {
				ToastUtil.showShortToast(CompleteUserinfoActivity.this, netModel.getContent());
			}
		}

		@Override
		public void onErrorResponse(VolleyError error) {
			// 隐藏提示框
			sendEmptydMessage(MSG_LOGIN_HIDE_LOADING);
			ToastUtil.showShortToast(CompleteUserinfoActivity.this, "网络异常");
		}
	}

	class GetAllTagResponseListenner
			implements com.android.volley.Response.Listener<String>, com.android.volley.Response.ErrorListener {

		@Override
		public void onErrorResponse(VolleyError error) {
			// TODO Auto-generated method stub
			ToastUtil.showShortToast(CompleteUserinfoActivity.this, "网络异常");
		}

		@Override
		public void onResponse(String response) {
			// TODO Auto-generated method stub
			System.out.println("tagsinfo:" + response);
			NetModel netModel = JsonUtil.parseObject(response, NetModel.class);
			if (netModel.getCode() == ConstanceUtil.REQUEST_SUCCESS) {
				alltags = JsonUtil.parseArray(netModel.getContent(), CHotTag.class);
				adapter2.setTags(alltags);
				adapter2.notifyDataSetChanged();
			}

		}

	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		switch (arg0.getId()) {
		case R.id.gridview1:
			alltags.add(mytags.get(arg2));
			mytags.remove(arg2);
			adapter1.notifyDataSetChanged();
			adapter2.notifyDataSetChanged();
			break;
		case R.id.gridview2:
			mytags.add(alltags.get(arg2));
			alltags.remove(arg2);
			adapter1.notifyDataSetChanged();
			adapter2.notifyDataSetChanged();
			break;
		}

	}

	@Override
	protected void onHandleMessage(Message msg) {
		switch (msg.what) {
		case MSG_LOGIN_SHOW_LOADING: {
			showProgressDialog(getString(R.string.login_loading));
			break;
		}
		case MSG_LOGIN_HIDE_LOADING: {
			hideProgressDialog();
			break;
		}
		default:
			break;
		}
	}
}
